package com.stadium.servlet.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stadium.bean.Equipment;
import com.stadium.bean.Match;
import com.stadium.bean.MatchToEquipment;
import com.stadium.bean.MatchToPlace;
import com.stadium.bean.Order;
import com.stadium.bean.Place;
import com.stadium.bean.PlaceOrder;
import com.stadium.repository.EquipmentRepository;
import com.stadium.repository.MatchRepository;
import com.stadium.repository.MatchToEquipmentRepository;
import com.stadium.repository.MatchToPlaceRepository;
import com.stadium.repository.OrderRepository;
import com.stadium.repository.PlaceOrderRepository;
import com.stadium.repository.PlaceRepository;
import com.stadium.servlet.MatchServlet;
import com.stadium.servlet.PlaceOrderServlet;

@Service(value = "MatchServlet")
public class MatchServletImpl implements MatchServlet {

	@Autowired
	private MatchRepository matchRepository;

	@Autowired
	private PlaceOrderServlet placeOrderServlet;

	@Autowired
	private EquipmentRepository equipmentRepository;

	@Autowired
	private PlaceRepository placeRepository;

	@Autowired
	private OrderRepository orderRepository;

	@Autowired
	private MatchToEquipmentRepository matchToEquipmentRepository;

	@Autowired
	private MatchToPlaceRepository matchToPlaceRepository;

	@Autowired
	private PlaceOrderRepository placeOrderRepository;

	public List<Match> findAll(Integer type) {
		if (type != null) {
			return setPlaceAndEquipent(matchRepository.findByStatus(type));
		} else {
			return setPlaceAndEquipent(matchRepository.findAll());
		}
	}

	@Transactional
	public void addMatch(Match match) throws ParseException {
		Order orders = new Order();
		PlaceOrder places = new PlaceOrder();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String date = df.format(new Date());// new Date()为获取当前系统时间
		match.setOrderTime(date);
		if (match.getPlacesId() != null) {
			places.setStartDate(match.getStartTime());
			places.setEndDate(match.getEndTime());
			places.setTelephone(match.getTelephone());
			places.setOrderTime(match.getOrderTime());
			places.setBorrowAccount(match.getBorrowAccount());
			places.setPlaceId(match.getPlacesId());
			places = placeOrderServlet.addPlaceOrder(places);
			addMatchToEquipment(match.getId(), places.getPlaceId());
		}
		if (match.getEquipId() != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d1 = sdf.parse(match.getOrderTime());
			orders.setOrderTime(d1);
			orders.setBorrowAccount(match.getBorrowAccount());
			orders.setTelephone(match.getTelephone());
			orders.setBorrowName(match.getBorrowName());
			orders.setNumber(match.getNumber());
			orders.setApplyReturn(0);
			orders.setIsReturn(0);
			orders = orderRepository.save(orders);
			addMatchToPlace(match.getId(), orders.getId());
		}
		match.setOrderId(orders.getId());
		match.setPlaceOrderId(places.getPlaceOrderId());
		match = matchRepository.save(match);
	}

	public void deleteMatch(Integer placeId) {
		matchRepository.delete(placeId);
	}

	public void setType(Integer placeId, Integer type) {
		Match match = matchRepository.findOne(placeId);
		System.out.println(match);
		match.setStatus(type);
		matchRepository.saveAndFlush(match);
	}

	// 保存一对多器材
	public void addMatchToEquipment(Integer matchId, Integer equipmentId) {
		MatchToEquipment matchToEquipment = new MatchToEquipment(matchId, equipmentId);
		matchToEquipmentRepository.save(matchToEquipment);
	}

	// 保存一对多器材
	public void addMatchToPlace(Integer matchId, Integer placeId) {
		MatchToPlace matchToPlace = new MatchToPlace(matchId, placeId);
		matchToPlaceRepository.save(matchToPlace);
	}

	public List<Match> setPlaceAndEquipent(List<Match> matchs) {
		for (Match match : matchs) {
			Order order = orderRepository.findOne(match.getOrderId());
			PlaceOrder placeOrder = placeOrderRepository.findOne(match.getPlaceOrderId());
			Equipment equipment = equipmentRepository.findOne(match.getEquipId());
			Place place = placeRepository.findOne(match.getPlacesId());
			match.setEquipment(equipment);
			match.setPlace(place);
			match.setOrders(order);
			match.setPlaces(placeOrder);
		}
		return matchs;
	}

	public Match setPlaceAndEquipent(Match match) {
		Order order = orderRepository.findOne(match.getOrderId());
		PlaceOrder placeOrder = placeOrderRepository.findOne(match.getPlaceOrderId());
		Equipment equipment = equipmentRepository.findOne(match.getEquipId());
		Place place = placeRepository.findOne(match.getPlacesId());
		match.setEquipment(equipment);
		match.setPlace(place);
		match.setOrders(order);
		match.setPlaces(placeOrder);
		return match;
	}

	public List<Match> findAllByBorrowAccount(String name) {
		return setPlaceAndEquipent(matchRepository.findByBorrowAccount(name));
	}

	public Match findMatchByName(String name) {
		return setPlaceAndEquipent(matchRepository.findByMatchName(name));
	}

	public void updateMatchOrder(Match match) throws ParseException {
		Order orders = new Order();
		PlaceOrder places = new PlaceOrder();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String date = df.format(new Date());// new Date()为获取当前系统时间
		match.setOrderTime(date);
		match = matchRepository.saveAndFlush(match);
	}

	public Match findOne(Integer id) {
		return matchRepository.findOne(id);
	}

}
